Skip to main content
  1. SwiftUI in 100 Days Notes/

Day 47 - SwiftUI Milestone: Projects 7-9 Challange

Table of Contents

In this challenge we will build a complete app from scratch on our own.

This time our goal is to create a habit tracker app for people who want to keep track of how much they do certain things.

At a minimum, this means that there should be a list of all the activities they want to track and a form to add new activities (a title and a description should be enough).

For a bigger challenge, tapping on one of the activities should display a detail screen with a description. See the tips below for this challenging task. Also on this detail screen, there should be a button that increments the number of times they have completed as well as the number of times they have completed.

And if you really want to make the app useful, use Codable and UserDefaults to load and save all your data.

So, this app has three levels and you can choose how far you want to go depending on how much time you have and how much you want to push yourself. I still recommend you to at least try each level.

Tips #

  • Start with your data: define a struct that holds a single activity and a class that holds activities as an array
  • SwiftUI will need to use the @Observable macro of the class to monitor your data for changes.
  • Your master list and form should both be able to read the shared activity object.
  • Make sure your activity conforms to Identifiable to avoid problems.
  • Present your insert form using sheet() and the activity detail view using NavigationLink.

Making a button to increase the number of completions will force you, because you need to modify the passed activity. If you get stuck, the easiest approach is the following;

  1. Make the Struct compatible with Equatable. You don’t need anything special here, just add Equatable after Codable and Identifiable.
  2. Pass both the selected activity and the @Observable class to the detail view.
  3. When the increment button is tapped, copy the current activity and add 1 to the completion count.
  4. Use firstIndex(of:) to find where the previous activity is in the class’s array, then change it to the new activity. (Something like data.activities[index]=newActivity will work. This requires Equatable conformance in step 1).

SwiftUI Habit Tracker App


You can also read this article in Turkish.
Bu yazıyı Türkçe olarak da okuyabilirsiniz.

This article contains the notes I took for myself from the articles found at SwiftUI Day 47. Please use the link to follow the original lesson.